// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        waitingNode:{
            default:null,
            type:cc.Node
        },
        panel1:{
            default:null,
            type:cc.Node,
        },
        panel2:{
            default:null,
            type:cc.Node,
        },
        sv1:{
            default:null,
            type:require("BuzyScrollView")
        },

        sv2:{
            default:null,
            type:require("BuzyScrollView")
        },

        panel1Url:{
            default:"",
        },

        panel2Url:{
            default:"",
        },

        tabButton1:{
            default:null,
            type:cc.Node
        },

        tabButton2:{
            default:null,
            type:cc.Node
        },

        myRankLabel:{
            default:null,
            type:cc.Label
        },

        _array1:null,
        _rank1:0,
        _array2:null,
        _rank2:0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.waitingNode.active = false
        this.panel1.active = false
        this.panel2.active = false
    },

    start () {

    },

    // update (dt) {},

    onRankWithTwoTabTabsClicked:function (event) {
        let btnNode = event.target
        if(btnNode === this.tabButton1){
            this.panel1.active = true
            this.panel2.active = false
            this.showPanel(1)
        }else if(btnNode === this.tabButton2){
            this.panel1.active = false
            this.panel2.active = true
            this.showPanel(2)
        }
    },

    async showPanel (number) {
        if(number === 1){
            this.panel1.active = true
            this.panel2.active = false

            if(!this._array1){
                this.waitingNode.active = true
                // this.sv1.clearBuzyScrollView()
                let result = await cc.gg.httpUtil.httpGet(this.panel1Url)//'/rank/daily/get'
                if(!result){
                    cc.gg.toastMgr.showToast("Load rank failed.")
                    return
                }
                let rank = result.data ? result.data.ranks : []
                let mRank = result.data ? result.data.mRank : 0
                this.waitingNode.active = false

                this._array1 = rank
                this._rank1 = mRank
                this.initSv(this.sv1,this._array1)
            }

            this.initSelfRank(this._rank1)
        }else if(number === 2){
            this.panel1.active = false
            this.panel2.active = true

            if(!this._array2){
                this.waitingNode.active = true
                // this.sv2.clearBuzyScrollView()
                let result = await cc.gg.httpUtil.httpGet(this.panel2Url)//'/rank/daily/get'
                if(!result){
                    cc.gg.toastMgr.showToast("Load rank failed.")
                    return
                }
                let ranks = result.data ? result.data.ranks : []
                let mRank = result.data ? result.data.mRank : 0
                this.waitingNode.active = false

                this._array2 = ranks
                this._rank2 = mRank
                this.initSv(this.sv2,this._array2)
            }

            this.initSelfRank(this._rank2)
        }else {
            cc.error("show panel with unknown number:",number)
        }
    },

    initSv:function (busySv,array) {
        cc.log("列表将要显示数据：",array)
        busySv.clearBuzyScrollView()
        busySv.initialize(array)
    },

    initSelfRank:function (rank) {
        cc.log("自己的名次：",rank)
        if(!rank || rank === "0") rank = "999+"
        this.myRankLabel.string = rank
    }


});
